<?php
defined('PHPFOX') or exit('NO DICE!');

class Hoidap_Component_Controller_Mypost extends Phpfox_Component
{
	
	public function process()
	{	
        $this->template()->setHeader(array( 
            'script.js' => 'module_hoidap' //sample.js de trong thu muc (getidcategor/static/jscript/sample.js)
        ));
        //$iduser = $this->request()->get('iduser');
        $iduser=$this->request()->get('iduser',0);
        $idcat = $this->request()->get('idcategory',0);
		//echo $idcat = $this->request()->get('req4');
        
        $nameCategory = PHPFOX::getService('hoidap.mypost')->getNameCategory($idcat);
        $this->template()->assign('nameCategory', $nameCategory);
        
        //Lay tat ca category trong bang hoidap_category
        $data = PHPFOX::getService('hoidap.mypost')->getCategory();
        $this->template()->assign('Category', $data);
        $hostname = $_SERVER['HTTP_HOST']; 
        $this->template()->assign('Link', 'hoidap/mypost/iduser_'.$iduser.'/idcategory_');
        
       
        //echo time();
       // echo date('d-m-Y',1365496437);
        
        $soLuongCauHoi = PHPFOX::getService('hoidap.mypost')->soLuongCauHoi($iduser,$idcat);
        $soCauHoi1Trang = 10;
        $soTrang = ceil($soLuongCauHoi/$soCauHoi1Trang);
        $trangChon = $this->request()->get('page');
        $trangChon = min($trangChon, $soTrang);
        $trangChon = max($trangChon,1);
        if($idcat>0)
            $link = phpfox::getParam('core.path'). 'hoidap/mypost/iduser_'.$iduser.'/idcategory_'.$idcat.'/page';
        if($idcat==0)
            $link = phpfox::getParam('core.path'). 'hoidap/mypost/iduser_'.$iduser.'/page';
        
        //Hien thi so luong cau hoi tm liem duoc
        $this->template()->assign('SoLuongCauHoi', $soLuongCauHoi);

		
		
		
		//$data = Phpfox::getService('hoidap.mypost')->showCauhoiByParentCat(1);
		//print_r ($data);

		
        //Hien thi cau hoi theo user va idCat
        if($this->request()->get('idcategory') != "")
        {
            $data = Phpfox::getService('hoidap.mypost')->getMyPost($iduser,$idcat,(($trangChon-1)*$soCauHoi1Trang),$soCauHoi1Trang);
			
			for($i=0; $i<count($data); $i++)
			{
				$strTitle = Phpfox::getService('hoidap.mypost')->bodau($data[$i]['title']);
				if(strlen($strTitle) > 55)
				{
					$data[$i]['title_short'] = substr($data[$i]['title'],0,55);
					$data[$i]['title_short'] .= '...';
				}
				else
					$data[$i]['title_short'] = $data[$i]['title'];
			}
			$this->template()->assign('list_cauhoi', $data);
        }
        else
        {
            $data = Phpfox::getService('hoidap.mypost')->getMyPost($iduser,0,(($trangChon-1)*$soCauHoi1Trang),$soCauHoi1Trang); 
            
			
			for($i=0; $i<count($data); $i++)
			{
				$strTitle = Phpfox::getService('hoidap.mypost')->bodau($data[$i]['title']);
				if(strlen($strTitle) > 55)
				{
					$data[$i]['title_short'] = substr($data[$i]['title'],0,100);
					$data[$i]['title_short'] .= '...';
				}
				else
					$data[$i]['title_short'] = $data[$i]['title'];
			}
			$this->template()->assign('list_cauhoi', $data);
        }

        //Phan Trang
        if($soTrang>1)
        {
            $page = array();
            
            if(2 <= $soTrang && $soTrang <= 5)
            {
                for($i=1; $i<=$soTrang; $i++)
                {
                    $page[$i]['numPage'] = $i;
                    $page[$i]['linkPage'] = $link.'_'.$i;
                    if($i==$trangChon)
                         $page[$i]['selectedPage'] = "class='page_selected'";
                }
            }
            else
            {
                if($trangChon > 3 && $trangChon <= ($soTrang-3))
                {
                    $page[-1]['numPage'] = 'Trang dau';
                    $page[-1]['linkPage'] = $link.'_1';
                    $page[-2]['numPage'] = '<<';
                    $page[-2]['linkPage'] = $link.'_'.max(1,($trangChon-5));
                        
                    for($i = ($trangChon-2); $i <= ($trangChon+2); $i++)
                    {
                        $page[$i]['numPage'] = $i;
                        $page[$i]['linkPage'] = $link.'_'.$i;
                        if($i==$trangChon)
                             $page[$i]['selectedPage'] = "class='page_selected'";
                    }
                    $page[-3]['numPage'] = '>>';
                    $page[-3]['linkPage'] = $link.'_'.min($soTrang,($trangChon+5));
                    $page[-4]['numPage'] = 'Trang cuoi';
                    $page[-4]['linkPage'] = $link.'_'.$soTrang;
                }
                
                if($trangChon <= 3)
                {
                    for($i = 1; $i <= 5; $i++)
                    {
                        if($i==$trangChon)
                        {
                            $page[$i]['numPage'] = $i;
                            $page[$i]['linkPage'] = "javascript:;";
                            $page[$i]['selectedPage'] = "class='page_selected'";
                        }
                        else
                        {
                            $page[$i]['numPage'] = $i;
                            $page[$i]['linkPage'] = $link.'_'.$i;
                        }
                    }
                    $page[-3]['numPage'] = '>>';
                    $page[-3]['linkPage'] = $link.'_'.min($soTrang,($trangChon+5));
                    $page[-4]['numPage'] = 'Trang cuoi';
                    $page[-4]['linkPage'] = $link.'_'.$soTrang;
                }
                if($trangChon > ($soTrang-3))
                {
                    $page[-1]['numPage'] = 'Trang dau';
                    $page[-1]['linkPage'] = $link.'_1';
                    $page[-2]['numPage'] = '<<';
                    $page[-2]['linkPage'] = $link.'_'.max(1,($trangChon-5));
                        
                    for($i = ($trangChon-2); $i <= ($soTrang); $i++)
                    {
                        if($i==$trangChon)
                        {
                            $page[$i]['numPage'] = $i;
                            $page[$i]['linkPage'] = "javascript:;";
                            $page[$i]['selectedPage'] = "class='page_selected'";
                        }
                        else
                        {
                            $page[$i]['numPage'] = $i;
                            $page[$i]['linkPage'] = $link.'_'.$i;
                        }
                    }
                }
            }
            $this->template()->assign('pageActive', "Trang $trangChon/$soTrang");
            $this->template()->assign('page', $page);
        
        }
       
 
	}  
}

?>
